/*
 * Copyright 2009-2009 Tanesha Project. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, 
 *    	 this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *    	 this list of conditions and the following disclaimer in the documentation 
 *    	 and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those 
 * of the authors and should not be interpreted as representing official policies, 
 * either expressed or implied, of the Tanesha Project.
 */
package com.google.api.chart;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 * Helper for wrapping dataseries into a scaling function.
 * 
 * @author sorenad
 */
public final class DataScalingHelper {

	/**
	 * Create a text scaling [0, 100] for a dataseries.
	 * @param inner
	 * @return
	 */
	public static DataSeriesMaker textScaling(DataSeriesMaker inner) {
		return new CustomScalingDataSeriesMaker(100, inner);
	}
	/**
	 * Create a simple scaling [0, 61] for a dataseries.
	 * @param inner
	 * @return
	 */
	public static DataSeriesMaker simpleScaling(DataSeriesMaker inner) {
		return new CustomScalingDataSeriesMaker(61, inner);
	}
	/**
	 * Create a custom scaling [0, scalingValue] for a dataseries
	 * @param scalingValue the value to scale with [0, scalingValue]
	 * @param inner
	 * @return
	 */
	public static DataSeriesMaker customScaling(double scalingValue, DataSeriesMaker inner) {
		return new CustomScalingDataSeriesMaker(scalingValue, inner);
	}
	/**
	 * Create a custom scaling [0, scalingValue] for a dataseries
	 * @param scalingValue the value to scale with [0, scalingValue]
	 * @param inner
	 * @return
	 */
	public static DataSeriesMaker minmaxScaling(double minValue, double maxValue, DataSeriesMaker inner) {
		return new MinMaxScalingDataSeriesMaker(minValue, maxValue, inner);
	}
	/**
	 * Create an extended scaling [0, 4095] for a dataseries.
	 * @param inner
	 * @return
	 */
	public static DataSeriesMaker extendedScaling(DataSeriesMaker inner) {
		return new CustomScalingDataSeriesMaker(4095, inner);
	}
	private static class CustomScalingDataSeriesMaker implements DataSeriesMaker {
		private DataSeriesMaker inner;
		private double scalingValue;
		public CustomScalingDataSeriesMaker(double scalingValue, DataSeriesMaker inner) {
			this.scalingValue = scalingValue;
			this.inner = inner;
		}
		@Override
		public BigDecimal[] getData() {
			return scaleWithinRange(scalingValue, inner.getData());
		}
	}
	private static class MinMaxScalingDataSeriesMaker implements DataSeriesMaker {
		private DataSeriesMaker inner;
		private BigDecimal minValue;
		private BigDecimal maxValue;
		public MinMaxScalingDataSeriesMaker(double minValue, double maxValue, DataSeriesMaker inner) {
			this.minValue = BigDecimal.valueOf(minValue);
			this.maxValue = BigDecimal.valueOf(maxValue);
			this.inner = inner;
		}
		@Override
		public BigDecimal[] getData() {
			return scaleWithinRange(minValue, maxValue, BigDecimal.valueOf(100), inner.getData());
		}
	}
	private DataScalingHelper() {
	}
	

	public static BigDecimal[] scaleWithinRange(final BigDecimal minValue, final BigDecimal maxValue, final BigDecimal scalingValue, BigDecimal[] data) {
		if (data == null)
			return null;
		
		final BigDecimal maxMinValue = maxValue.subtract(minValue);
		
		final MathContext ctx = new MathContext(4, RoundingMode.HALF_DOWN);
		
		final BigDecimal[] scaled = new BigDecimal[data.length];
		for (int i = 0; i < data.length; i++)
			scaled[i] = data[i].subtract(minValue, ctx).divide(maxMinValue, ctx).multiply(scalingValue, ctx);
		
		return scaled;
	}
	
	private static BigDecimal[] findMinMax(BigDecimal[] data) {
		BigDecimal minValue = null;
		BigDecimal maxValue = null;
		
		for (BigDecimal e : data) {
			if (minValue == null || e.compareTo(minValue) < 0)
				minValue = e;
			if (maxValue == null || e.compareTo(maxValue) > 0)
				maxValue = e;
		}

		return new BigDecimal[]{ minValue, maxValue };
	}

	public static BigDecimal[] scaleWithinRange(double scalingValueD, BigDecimal[] data) {
		if (data == null)
			return null;

		BigDecimal[] minmax = findMinMax(data);

		return scaleWithinRange(minmax[0], minmax[1], BigDecimal.valueOf(scalingValueD), data);
	}
}
